package com.edu.dao.impl;

import com.edu.bean.User;
import com.edu.dao.BaseDao;
import com.edu.dao.UserDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    public int selectByUserName(String userName) {
        String sql="select userName from user where userName='"+userName+"'";
        Connection conn= BaseDao.getConn();//打开连接
		PreparedStatement ps= null;
		ResultSet rs=null;
		try {
			ps= conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()) {
                System.out.println("有账号");
                return 1;
			}
            return  -1;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(conn, ps,rs);
		}

        return 0;
    }

    public User selectByUser(String userName, String userPass) {
		String sql = "select * from user where userName=? and userPass=?";
		Object[] obj={userName,userPass};
		Connection conn = BaseDao.getConn();//打开连接
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			for (int i = 0; i < obj.length; i++) {
				ps.setObject(i+1, obj[i]);
			}
			rs = ps.executeQuery();
			while (rs.next()) {
				User user = new User();
				user.setUserId(rs.getInt("userId"));
				user.setUserName(rs.getString("userName"));
				user.setUserPass(rs.getString("userPass"));
				user.setNickName(rs.getString("nickName"));
				return user;
			}
			User user01 = new User();
			user01.setUserId(-1);
			return user01;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(conn, ps, rs);
		}

	return null;
	}



}
